package org.qiyu.live.im.core.server.common;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * Created with IntelliJ IDEA.
 * Description: 编码器
 * User: sfx
 * Date: 2023-10-06
 * Time: 20:49
 */
public class TcpImEncoder extends MessageToByteEncoder<Object> {
    @Override
    protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {
        // 都要转换成ImMsg
        ImMsg imMsg = (ImMsg) msg;
        // 魔数
        out.writeShort(imMsg.getMagic());
        // code
        out.writeInt(imMsg.getCode());
        // len
        out.writeInt(imMsg.getLen());
        // body
        out.writeBytes(imMsg.getBody());
    }
}